package cn.sise.edu.crm.web.controller;

import cn.sise.edu.crm.domain.Department;
import cn.sise.edu.crm.domain.Employee;
import cn.sise.edu.crm.domain.JsonResult;
import cn.sise.edu.crm.domain.Role;
import cn.sise.edu.crm.query.EmployeeQueryObject;
import cn.sise.edu.crm.query.PageResult;
import cn.sise.edu.crm.service.IDepartmentService;
import cn.sise.edu.crm.service.IEmployeeService;
import cn.sise.edu.crm.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * Created by chen on 2019/11/3.
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;
    @RequestMapping("/list")
    @RequiresPermissions(value = {"员工页面", "employee:list"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo pageInfo = employeeService.query(qo);
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("depts",departments);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("qo",qo);
        return "employee/list";
    }
    @RequestMapping("/export")
    @RequiresPermissions(value = {"员工导出", "employee:export"},logical = Logical.OR)
    public void export(HttpServletResponse response) throws Exception {
        response.setHeader("content-disposition","attachment;filename=employee.xls");
        Workbook workbook = employeeService.export();
        workbook.write(response.getOutputStream());
    }
    @RequestMapping("/temp")
    public void temp(HttpServletResponse response) throws Exception {
        response.setHeader("content-disposition","attachment;filename=employee.xls");
        Workbook workbook = employeeService.temp();
        workbook.write(response.getOutputStream());
    }
    @RequestMapping("/imports")
    @RequiresPermissions(value = {"员工导入", "employee:imports"},logical = Logical.OR)
    public String imports(MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();
        employeeService.imports(inputStream);
        return "redirect:/employee/list.do";

    }

    @RequestMapping("/input")
    @RequiresPermissions(value = {"员工信息页面", "employee:input"},logical = Logical.OR)
    public String input(Long id, Model model) {
        List<Department> departments = departmentService.selectAll();
        List<Role> roles = roleService.selectAll();
        model.addAttribute("depts",departments);
        model.addAttribute("roles",roles);
        if (id != null) {
            Employee employee = employeeService.selectById(id);
            model.addAttribute("employee", employee);
            return "/employee/input";
        } else {
            return "/employee/input";
        }
    }
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"员工删除", "employee:delete"},logical = Logical.OR)
    public JsonResult delete(Long id){
        employeeService.delete(id);
        try {
            employeeService.delete(id);
        }catch (Exception e){
            return new JsonResult("删除失败",false);
        }

        return new JsonResult("删除成功",true);
    }
    @RequestMapping("/butchDelete")
    @ResponseBody
    @RequiresPermissions(value = {"员工批量删除", "employee:butchDelete"},logical = Logical.OR)
    public JsonResult butchDelete(Long[] ids){
        try {
            employeeService.deleteByEmployeeIds(ids);
        }catch (Exception e){
            return new JsonResult("删除失败",false);
        }

        return new JsonResult("删除成功",true);
    }
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"员工保存或者更新", "employee:saveOrUpdate"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Employee employee,Long[] los){
        try {
            if(employee.getId() != null){
                employeeService.update(employee,los);

            }else{
                employee.setPassword( new SimpleHash("MD5", employee.getPassword(),employee.getName()).toString());
                employeeService.insert(employee,los);
            }
        }catch (Exception e){
            return new JsonResult("操作失败",false);
        }

        return new JsonResult("操作成功",true);
    }
}
